<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cron extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('user');
		$this->load->model('assessments');
		$this->load->model('settings');
	}

	public function index() {
		echo '<a href="' . site_url("cron/peer_review_warning") . '"" >Peer Review Warnings</a><br />';
		echo '<a href="' . site_url("cron/assessment_reminder") . '"" >Assessment Reminders</a>';
	}

	public function peer_review_warning() {
		// Check the correct security key has been used
		if ($this->uri->segment(3) != $this->settings->get_setting('cron_key')) {
			echo "Invalid security key.";
		} else {
			// Get previous week
			$first_week = $this->settings->get_setting('first_week');
			$weeks = $this->settings->get_setting('weeks');
			$current_week = date("W");
			$previous_week = $current_week--;
			$friendly_previous_week = $previous_week - $first_week;

			// Email settings
			$enabled = $this->settings->get_setting('cron_enabled');

			// Get a list of students
			$students = $this->user->get_user_data(null, 'student');

			foreach ($students as $key => $student) {
				// Check they did their peer review last week
				if ( ! $this->assessments->_check_peer_review($student['user_id'],$previous_week) ) {
					echo '<strong style="color:red;">AT-RISK STUDENT: ' . $student["first_name"] . ' ' . $student["last_name"] . ' (' . $student['qut_id'] . ') ' . '</strong><p style="margin:0;">Student did not submit a peer review for week ' . $friendly_previous_week . '.</p>';

					if ($enabled) {
						$data['first_name'] = $student["first_name"];
						$data['email'] = $student["email"];
						if ( $this->_email_user_warning($data) ) {
							echo '<p style="margin-top:0;">Warning email sent to student.</p>';
						} else {
							echo '<p style="margin-top:0;">Error sending warning email to student.</p>';
						}

					} else {
						echo '<p style="margin-top:0;">Warning email was not sent.</p>';
					}
				}
			}

		}
	}

	public function assessment_reminder() {
		if ($this->uri->segment(3) != $this->settings->get_setting('cron_key')) {
			echo "Invalid security key.";
		} else {
			// Current data
			$todays_date = date("Y-m-d", strtotime("+3 days"));
			$assessments = $this->assessments->get_all_assessment_items();
			foreach ($assessments as $key => $assessment) {
				$due_date = explode(' ', $assessment['due_date']);
				$due_date = $due_date[0];
				
				$today = strtotime($todays_date);
				$due = strtotime($due_date);

				if ($today > $due) {
					// Send reminder
					echo '<strong style="color:red;">' . $assessment["assessment_title"] . ' is due within 3 days</strong>';
					if ($this->_email_reminder($assessment["assessment_title"])) {
						echo '<p style="margin-top:0;">Email reminder sent to all students.</p>';
					} else {
						echo '<p style="margin-top:0;">Error sending email reminder to students.</p>';
					}
				}
			}
		}
	}

	private function _email_user_warning($data) {

		$message = 'Hi '. $data['first_name'] .', it appears you did not submit any peer reviews last week. Don\'t forget to submit them next week.';

		// Load the CI email library
		$this->load->library('email');

		$this->email->from($this->settings->get_setting('administrator_email'), $this->settings->get_setting('administrator_name'));
		$this->email->to($data['email']); 
		$this->email->subject("Peer Review Warning");
		$this->email->message($message);	

		if ( $this->email->send() ) {  
			return TRUE;
		} else {
			return FALSE;
		}	
   }

   	private function _email_reminder($assessment_title) {

		$message = 'Just a friendly reminder that ' . $assessment_title . 'is due in less than three days.';

		// Load the CI email library
		$this->load->library('email');	

		$students = $this->user->get_user_data(null, 'student');
		foreach ($students as $key => $student) {
			$this->email->from($this->settings->get_setting('administrator_email'), $this->settings->get_setting('administrator_name'));
			$this->email->subject("Assessment Reminder");
			$this->email->message($message);
			$this->email->to($student['email']); 
			$this->email->send();
		}
		return true;
   }

}

/* End of file cron.php */
/* Location: ./application/controllers/cron.php */